/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * TestCourses.java
 *
 * Created on Jul 5, 2011, 8:55:40 PM
 */

package gem.gui.courses;

import gem.controller.Controller;
import gem.controller.Command;
import gem.controller.DepartmentActionListener;
import gem.model.core.Course;
import gem.model.core.DBEntity;
import gem.model.core.Department;
import gem.model.core.DepartmentCatalogueModel;
import gem.model.core.DepartmentListener;
import gem.model.core.ScienceDomain;
import gem.model.core.SimpleDepartmentCataloqueModel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author Dusan
 */
public class TestCourses extends javax.swing.JFrame implements DepartmentListener, ActionListener, DepartmentActionListener
{

    /** Creates new form TestCourses */
    public TestCourses( DepartmentCatalogueModel model ) throws SQLException
    {
        initComponents();
        this.model = model;
        setComponents();
    }

    private DepartmentCatalogueModel model;
    private java.util.Collection<DBEntity> ents;

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        m_Tree = new javax.swing.JTree();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jScrollPane1.setViewportView(m_Tree);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 421, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(302, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 454, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    public void dataChanged()
    {
        System.out.println("Poruka primljena");
        try {
            ents = (java.util.Collection<DBEntity>) (java.util.Collection<?>)model.listDepartments( false );
        } catch (SQLException ex) {
            Logger.getLogger(TestCourses.class.getName()).log(Level.SEVERE, null, ex);
        }
        m_Tree.setModel( new CoursesTreeModel().setContent( ents ));
    }

    public void departmentActionPerformed( DBEntity sender, String command )
    {
        System.out.println("Command recieved: " + sender + " " + command );
        String param = null;
        boolean proceed = false;
        
    }

    
    public void actionPerformed(ActionEvent e)
    {
        boolean proceed = false;
        String param = "";
        Object selected = m_Tree.getLastSelectedPathComponent();
        System.out.println( e.getActionCommand() + " " + selected.getClass() );

        if( e.getActionCommand().equals( Common.COMMAND_ADD ) )
        {
            if( ( param = JOptionPane.showInputDialog("Unesite ime")) != null )
            {
                if (!param.equals(""))
                {
                    try
                    {
                        switch( ((DBEntity)selected).getType() )
                        {
                        case DEPARTMENT:
                            Department p = (Department)selected;
                            ScienceDomain sd = new ScienceDomain().setTitle(param);
                            p.addScienceDomain(sd);
                            model.addNewScienceDomain(sd);
                            break;
                        case SCIENCEDOMAIN:
                            ScienceDomain sp = (ScienceDomain)selected;
                            Course c = new Course().setTitle(param);
                            sp.addCourse(c);
                            model.addNewCourse(c);
                            break;
                        case NONE:
                            model.addNewDepartment(new Department().setTitle(param));
                        }
                    }
                    catch( SQLException ex )
                    {
                        ex.printStackTrace();
                    }
                }
            }
        }
        else if( e.getActionCommand().equals( Common.COMMAND_REMOVE ) )
        {
            proceed = ( JOptionPane.showConfirmDialog(null, "Da li ste sigurni da želite da obrišete?") == JOptionPane.OK_OPTION );
        }
        else if( e.getActionCommand().equals( Common.COMMAND_RENAME ) )
        {
            if( ( param = JOptionPane.showInputDialog("Unesite novo ime")) != null )
            {
                if (!param.equals(""))
                {
                    switch( ((DBEntity)selected).getType() )
                    {
                    case DEPARTMENT:
                       
                    }
                }
            }
        }

        if( proceed )
        {
            //AppController.getInstance().execute( new Command( (DBEntity)m_Tree.getLastSelectedPathComponent(), e.getActionCommand(), param ) );

        }
    }

    private void setComponents() throws java.sql.SQLException
    {
        ents = (java.util.Collection<DBEntity>)(java.util.Collection<?>)model.listDepartments( false );

        m_Tree.setModel( new CoursesTreeModel().setContent( ents ));

        final DepartmentActionListener dal = this;

        m_Tree.addMouseListener( new MouseAdapter()
        {
            @Override
            public void mouseClicked(MouseEvent e)
            {
                if( e.getButton() == e.BUTTON3 )
                {
                    // enabling right click selection
                    int row = m_Tree.getClosestRowForLocation(e.getX(), e.getY());
                    m_Tree.setSelectionRow(row);
                    PopupMenu.getMenu((DBEntity)m_Tree.getSelectionPath().getLastPathComponent(), dal ).show(m_Tree, e.getX(), e.getY());
                }
            }
        }
        );

        model.addDepartmentListener(this);
    }

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) throws SQLException
    {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run()
            {
                try
                {
                    new TestCourses(new SimpleDepartmentCataloqueModel()).setVisible(true);
                }
                catch (SQLException ex)
                {
                    ex.printStackTrace();
                }
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTree m_Tree;
    // End of variables declaration//GEN-END:variables

}
